<template>
    <div class="app-container">
        <el-row style="height: 100%">
            <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
                <el-form-item label="套餐名称">
                    <el-input clearable v-model="queryParams.comboName" placeholder="请输入套餐名称" clearable
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="卡号">
                    <el-input clearable v-model="queryParams.cardNumber" placeholder="请输入卡号" clearable
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="所在区域" prop="area">
                    <el-cascader style="width: 230px;" clearable placeholder="请选择" size="small" v-model="location"
                        :options="areaSelectData" :props="{
                            label: 'Name',
                            value: 'Name',
                            children: 'ChildList',
                            checkStrictly: false
                        }"></el-cascader>
                </el-form-item>
                <el-form-item label="详细地址">
                    <el-input clearable v-model="queryParams.detailAddress" placeholder="请输入详细地址" clearable
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="收货人姓名">
                    <el-input clearable v-model="queryParams.receiverName" style="width: 120px;" placeholder="收货人姓名"
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="收货人手机号">
                    <el-input clearable v-model="queryParams.receiverPhone" placeholder="收货人手机号"
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="物流状态">
                    <el-select clearable v-model="queryParams.status" placeholder="请选择" style="width: 150px;">
                        <el-option v-for="item in logisticsStatusOpt" :key="item.value" :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="起止时间">
                    <el-date-picker v-model="dateRange" style="width: 350px;" size="small" @change="changeDate"
                        value-format="yyyy-MM-dd HH:mm:ss" type="datetimerange" range-separator="-"
                        start-placeholder="开始时间" end-placeholder="结束时间" clearable></el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
            <el-table v-loading="loading" height="90%" :data="userList">
                <el-table-column label="套餐卡号" align="center" prop="cardNumber" />
                <el-table-column label="套餐名称" align="center" prop="comboName" />
                <el-table-column label="用户姓名" align="center" prop="receiverName" />
                <el-table-column label="手机号码" align="center" prop="receiverPhone" />
                <el-table-column label="详细地址" align="center" prop="province">
                    <template slot-scope="scope">
                        <span>{{ scope.row.province }}{{ scope.row.city }}{{ scope.row.district
                        }}{{ scope.row.detailAddress }} </span>
                    </template>
                </el-table-column>
                <el-table-column label="订单状态" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.status === 1 ? '未核销' : scope.row.status === 2 ?
                            '已核销' :
                            scope.row.status === 2 ? '已作废' : '' }} </span>
                    </template>
                </el-table-column>
                <el-table-column label="单号" align="center" prop="trackingNumber" />
                <el-table-column label="兑换时间" align="center" width="150" prop="exchangeTime" />
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button type="primary" size="small" @click="open(scope.row)">发货</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList" />
        </el-row>
    </div>
</template>

<script>
import { getComboListApi, comboUpdateOrApi } from "@/api/index.js"
import { cityData } from "@/utils/select_area.js"
console.log(cityData, 'areaSelectData');

export default {
    name: "mallOrder",
    components: {},
    data() {
        return {
            location: [],
            areaSelectData: cityData,//省市区三级联动数据
            // 遮罩层
            loading: true,
            // 总条数
            total: 0,
            // 用户表格数据
            userList: null,
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            // 表单参数
            form: {},
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                comboName: undefined,
                cardNumber: undefined,
                batch: undefined,
                status: undefined,
                startDate: "",
                endDate: "",
                province: undefined,
                city: undefined,
                district: undefined,
                detailAddress: undefined,
                receiverPhone: undefined,
                receiverName: undefined,
            },
            dateRange: [],
            // optionsStatus: [
            //     { value: 1, label: "未核销" },
            //     { value: 2, label: "已核销" },
            //     { value: 3, label: "作废" },
            // ],
            logisticsStatusOpt: [
                { value: 0, label: "待发货" },
                { value: 1, label: "已发货" },
                { value: 2, label: "已签收" },
            ],
        }
    },
    watch: {

    },
    created() {
        this.getList()
    },
    methods: {
        changeDate() {
            console.log(this.dateRange, 'dateRange');
            if (this.dateRange) {

            } else {
                this.queryParams.startDate = "";
                this.queryParams.endDate = "";
            }
        },
        handleChange(value) {
            console.log(value, 'value');

            if (value.length > 0) {
                this.queryParams.province = CodeToText[value[0]];
            } else {
                this.queryParams.province = undefined;
            }
            if (value.length > 1) {
                this.queryParams.city = CodeToText[value[1]];
            } else {
                this.queryParams.city = undefined;
            }
            if (value.length > 2) {
                this.queryParams.district = CodeToText[value[2]];
            } else {
                this.queryParams.district = undefined;
            }
        },
        /** 查询用户列表 */
        getList() {
            this.loading = true
            getComboListApi(this.queryParams).then(response => {
                console.log(response, 'response');
                if (response.data && response.data.list) {
                    this.userList = response.data.list;
                } else {
                    this.userList = []
                }
                this.total = response.data.total
                this.loading = false
            }).catch(() => {
                this.loading = false
            })
        },
        open(item) {
            this.$prompt('请输入快递单号', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /.+/, // 只要非空即可
                inputErrorMessage: '快递单号不能为空'
            }).then(({ value }) => {
                this.updateTrackingNumber(item.id, value);
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消输入'
                });
            });
        },
        // 更新快递单号
        async updateTrackingNumber(id, trackingNumber) {
            const res = await comboUpdateOrApi({
                id, trackingNumber,
                status: 1
            });
            if (res.code === 200) {
                this.$modal.msgSuccess("提交成功")
                this.getList();
            }
        },
        /** 搜索按钮操作 */
        handleQuery() {
            console.log(this.location, 'location');
            this.queryParams.province = this.location[0] ? this.location[0] : undefined;
            this.queryParams.city = this.location[1] ? this.location[1] : undefined;
            this.queryParams.district = this.location[2] ? this.location[2] : undefined;
            this.queryParams.pageNum = 1;
            console.log(this.dateRange, 'this.dateRange');
            if (this.dateRange && this.dateRange.length === 2) {
                this.queryParams.startDate = this.dateRange[0];
                this.queryParams.endDate = this.dateRange[1];
            }
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = [];
            this.location = [];
            // 查询参数
            this.queryParams = {
                pageNum: 1,
                pageSize: 10,
                comboName: undefined,
                cardNumber: undefined,
                batch: undefined,
                status: undefined,
                startDate: "",
                endDate: "",
                province: undefined,
                city: undefined,
                district: undefined,
                detailAddress: undefined,
                receiverPhone: undefined,
                receiverName: undefined,
            }
            this.resetForm("queryForm")
            this.handleQuery()
        },
    }
}
</script>
<style lang="scss" scoped>
.app-container {
    height: calc(100vh - 120px);
    // border: 1px solid #222;
}
</style>